package curves;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JComboBox;

import function.FunVariations;
import function.Function;
import function.Functions;
import function.SyntaxErrorException;


public class CurveBackup {

	public static void save(String[] fun_var_names, File file){
		try {
			FileWriter writer = new FileWriter(file);
			for(int i=0; i < fun_var_names.length; i++){
				writer.write(fun_var_names[i]+"\n");	
			}
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void restore(CurveFrameUser cfu, File file){
		final Color[] color_choice = {Color.black, Color.red, Color.blue, Color.cyan, Color.orange, Color.green, Color.darkGray};
		int i = 0;
		CurveTracerUser ctu = cfu.getTracer();
		JComboBox fun_cb = cfu.getControls().getPanelCalcul().getFunCb();
		fun_cb.removeAllItems();
		ctu.clear();
		try {
			FileReader reader = new FileReader(file);
			BufferedReader breader = new BufferedReader(reader);
			String s;
			while((s = breader.readLine()) != null){
				i++;
				try {
					Function f = Functions.parse(s);
					FunctionVariations fvar = new FunVariations(f, -2 * Math.PI, 2 * Math.PI);
					ctu.addFunctionVariations(fvar, s, color_choice[i % color_choice.length - 1] );
					fun_cb.addItem(s);
				} catch (SyntaxErrorException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			ctu.tabulateThemAll(20);
			reader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
